// 桂林：http://10.188.170.168
// 南宁：http://10.188.179.186

var axios = require("axios");
//固定登录信息
var data = JSON.stringify({
  CuCode: "001",
  UCode: "admin",
  UPwd: "123",
  imgcode1: "j33a",
});

var config = {
  method: "post",
  url: "http://10.188.170.168/Login/DoLogin",
  headers: {
    "Content-Type": "application/json",
  },
  data: data,
};

//1、登录
axios(config)
  .then(function (response) {
    console.log(JSON.stringify(response.data)); //登录结果
    const cookies = response.headers["set-cookie"]; 
    if (cookies && cookies.length > 0 && cookies[0].indexOf(";") > -1) {
      //登录成功的cookie键值对
      const cookiePair = cookies[0].split(";")[0]; //'.AspNetCore.Session=CfDJ8I%2FVuqRRFbxHu2M4jPrVkOthT46R9mW3wNLmy%2F7yaFTV2%2FcaG9eMgsaDyt%2BWeECjbGTQmFrM%2FCM%2FptkyGQXLs9I29I9RzIaqSjXAoAqiMZoGN8PfAkNSFg8DfCYiwvp8XQK%2F02OoIW2%2Fhp7rNoVnWb0qFmeqTB3jDM8Xgj75AiSz',
      //柜门样本：具体访问http://10.188.170.168:3000/json/locks
      // [
      //    {
      //       "id": "1",
      //       "BlId": 1,
      //       "DeviceId": "001",
      //       "mainboardno": 1,
      //       "lockno": 1,
      //       "czh": "2001",
      //       "occupied": false
      //     },
      //     {
      //       "id": "2",
      //       "BlId": 2,
      //       "DeviceId": "001",
      //       "mainboardno": 1,
      //       "lockno": 2,
      //       "czh": "2003",
      //       "occupied": false
      //     },
      //     ...
      // ]
      var door =
        '{\n    "BlId": 11,\n    "DeviceId": "001",\n    "mainboardno": 1,\n    "lockno": 11\n}';
      var config = {
        method: "post",
        url: "http://10.188.170.168/BoxsSet/OpenlockTest",
        headers: {
          "Content-Type": "application/json",
          Cookie: cookiePair,
        },
        data: door,
      };

      //2、开柜门
      axios(config)
        .then(function (response) {
          console.log(JSON.stringify(response.data));
        })
        .catch(function (error) {
          console.log(error);
        });
    }
  })
  .catch(function (error) {
    console.log(error);
  });
